package com.example.cloudnative.Controller;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpStatusCodeException;

@RestController
public class DemoController
{

    private final RateLimiter rateLimiter = RateLimiter.create(100.0);

    @GetMapping("/api/hello")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String hello()
    {
        if (!rateLimiter.tryAcquire(1))
            throw new HttpStatusCodeException(HttpStatus.TOO_MANY_REQUESTS)
            {
            };
        return "Hello World!(nju22)";
    }


}
